//
//  User.m
//  Trackit
//
//  Created by yaodaobin on 2017/4/2.
//  Copyright © 2017年 yaodaobin. All rights reserved.
//

#import "User.h"

@implementation User

- (id)initWithCoder:(NSCoder *)coder {
    if (self = [super init]) {
        self.userID = [coder decodeObjectForKey:@"UserID"];
        self.userName = [coder decodeObjectForKey:@"UserName"];
        self.realName = [coder decodeObjectForKey:@"RealName"];
        self.department = [coder decodeObjectForKey:@"Department"];
        self.phone = [coder decodeObjectForKey:@"Phone"];
        self.userDescription = [coder decodeObjectForKey:@"Description"];
        self.userRoleName = [coder decodeObjectForKey:@"UserRoleName"];
        self.password = [coder decodeObjectForKey:@"Password"];
        self.userRole = [[coder decodeObjectForKey:@"UserRole"] unsignedIntegerValue];
        self.intervalValue = [coder decodeObjectForKey:@"intervalValue"];
        self.distanceValue = [coder decodeObjectForKey:@"distanceValue"];
        self.localImageUrlPathDictionary = [coder decodeObjectForKey:@"localImageUrlPathDictionary"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeObject:_userID forKey:@"UserID"];
    [coder encodeObject:_userName forKey:@"UserName"];
    [coder encodeObject:_realName forKey:@"RealName"];
    [coder encodeObject:_department forKey:@"Department"];
    [coder encodeObject:_phone forKey:@"Phone"];
    [coder encodeObject:_userDescription forKey:@"Description"];
    [coder encodeObject:_userRoleName forKey:@"UserRoleName"];
    [coder encodeObject:_password forKey:@"Password"];
    [coder encodeObject:[NSNumber numberWithUnsignedInteger:_userRole] forKey:@"UserRole"];
    [coder encodeObject:_intervalValue forKey:@"intervalValue"];
    [coder encodeObject:_distanceValue forKey:@"distanceValue"];
    [coder encodeObject:_localImageUrlPathDictionary forKey:@"localImageUrlPathDictionary"];
}

- (instancetype)initWithData:(NSDictionary *)data {
    self = [super init];
    if (self) {
        self.userID = [NSString stringWithFormat:@"%ld",(long)[data[@"UserID"] integerValue]];
        self.userName = [CommonFunction getValueFromDic:data withKey:@"UserName"];
        self.realName = [CommonFunction getValueFromDic:data withKey:@"RealName"];
        self.department = [CommonFunction getValueFromDic:data withKey:@"Department"];
        self.phone = [CommonFunction getValueFromDic:data withKey:@"Phone"];
        self.userDescription = [CommonFunction getValueFromDic:data withKey:@"Description"];
        self.userRoleName = [CommonFunction getValueFromDic:data withKey:@"UserRoleName"];
        self.password = [CommonFunction getValueFromDic:data withKey:@"Password"];
        self.userRole = [data[@"UserRole"] unsignedIntegerValue];

        self.localImageUrlPathDictionary = [NSMutableDictionary dictionary];

        self.intervalValue = @"10.0";
        self.distanceValue = @"200.0";
    }
    return self;
}

- (NSMutableDictionary<NSString *,NSMutableArray *> *)localImageUrlPathDictionary {
    if (!_localImageUrlPathDictionary) {
        _localImageUrlPathDictionary = [NSMutableDictionary dictionary];
    }
    return _localImageUrlPathDictionary;
}

- (void)cleanOldCache {
    NSString *oldID = [[UserDefaultManagement instance] getLastLoginedUserID];
    if (![self.userID isEqualToString:oldID]) {
        //清除旧账号的所有缓存
        [APP_DELEGATE.user.localImageUrlPathDictionary removeAllObjects];
        [UserDefaultManagement instance].localDataDic = nil;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *path = [CommonFunction getCachePath];
        if ([fileManager fileExistsAtPath:path]) {
            [fileManager removeItemAtPath:path error:nil];
        }
    }
}

@end
